$(document).ready(function(){
    $.fn.dataTableExt.sErrMode = 'throw';

    get_daftar_pegawai();
    get_daftar_pegawai_sudah();
    getUsulan();


    $('#myTab a:first').tab('show');
    $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
      e.target; // activated tab
      e.relatedTarget; // previous tab
    });


    return false;
    
});

function get_daftar_pegawai() {
    $.getJSON('controller/cont.usulan_kppn.php', {aksi_daftar_pegawai: true}, function(data) {
        $('#list_pegawai tbody').html('');
        var tab = "";
        if (data.msg) {
            $.each(data.daftar, function(index, daftar) {
                tab = "<tr detailId=\""+ daftar.usulan.id +"\">";
                tab += "<td>" + daftar.usulan.nip + "</td>";
                tab += "<td>"+daftar.pegawai.nama+"</td>";
                tab += "<td>" + daftar.ref + "</td>";
                tab += "<td>" + daftar.tujuan + "</td>";
                tab += "<td>" + daftar.usulan.tglAwal + " s.d " + daftar.usulan.tglAkhir + "</td>";                
                tab += "<td align=\"center\"><a data-toggle=\"modal\" href=\"#myModalUpload\" onclick=\"uploadDokumen(" + daftar.usulan.id + ")\" class=\"btn btn-default btn-sm\" ><span class=\"glyphicon glyphicon-cloud-upload\"></span>&nbsp;Dokumen</a></td>";                            
                tab += "<td align=\"center\"><input id=\"selectPeg\" name=\"selectPeg\" type=\"checkbox\" /></td>";
                tab += "</tr>";
                //alert(tab);
                $('#list_pegawai tbody').append(tab);

            });

            $('#list_pegawai').dataTable({
                "sPaginationType": "bs_full",
                "bInfo": false,
                "bLengthChange": false,
                "iDisplayLength": 5,
              }); 
              $('#list_pegawai').each(function(){
                var datatable = $(this);
                // SEARCH - Add the placeholder for Search and Turn this into in-line form control
                var search_input = datatable.closest('.dataTables_wrapper').find('div[id$=_filter] input');
                search_input.attr('placeholder', 'Search');
                search_input.addClass('form-control input-sm');
                // LENGTH - Inline-Form control
                var length_sel = datatable.closest('.dataTables_wrapper').find('div[id$=_length] select');
                length_sel.addClass('form-control input-sm');
              });
        } else {
            tab += "<tr>"
            tab += "<td colspan=\"9\">Data Tidak Ada</td>";
            tab += "</tr>";
            $('#list_pegawai tbody').append(tab);
        }
    });
}


function get_daftar_pegawai_sudah() {
    $.getJSON('controller/cont.usulan_kppn.php', {aksi_daftar_pegawai_sudah: true}, function(data) {
        $('#list_pegawai2 tbody').html('');
        var tab = "";
        if (data.msg) {
            $.each(data.daftar, function(index, daftar) {
                tab = "<tr detailId=\""+ daftar.usulan.id +"\">";
                tab += "<td>" + daftar.usulan.nip + "</td>";
                tab += "<td>"+daftar.pegawai.nama+"</td>";
                tab += "<td>" + daftar.ref + "</td>";
                tab += "<td>" + daftar.tujuan + "</td>";
                tab += "<td>" + daftar.usulan.tglAwal + " s.d " + daftar.usulan.tglAkhir + "</td>";                
                tab += "<td align=\"center\"><a data-toggle=\"modal\" href=\"#myModalUpload\" onclick=\"uploadDokumen(" + daftar.usulan.id + ")\" class=\"btn btn-default btn-sm\" ><span class=\"glyphicon glyphicon-cloud-upload\"></span>&nbsp;Dokumen</a></td>";                            
                tab += "</tr>";
                //alert(tab);
                $('#list_pegawai2 tbody').append(tab);

            });

            $('#list_pegawai2').dataTable({
                "sPaginationType": "bs_full",
                "bInfo": false,
                "bLengthChange": false,
                "iDisplayLength": 5,
              }); 
              $('#list_pegawai2').each(function(){
                var datatable = $(this);
                // SEARCH - Add the placeholder for Search and Turn this into in-line form control
                var search_input = datatable.closest('.dataTables_wrapper').find('div[id$=_filter] input');
                search_input.attr('placeholder', 'Search');
                search_input.addClass('form-control input-sm');
                // LENGTH - Inline-Form control
                var length_sel = datatable.closest('.dataTables_wrapper').find('div[id$=_length] select');
                length_sel.addClass('form-control input-sm');
              });
        } else {
            tab += "<tr>"
            tab += "<td colspan=\"9\">Data Tidak Ada</td>";
            tab += "</tr>";
            $('#list_pegawai2 tbody').append(tab);
        }
    });
}

function ajaxFileUpload(){
    var id_usulan = $('#ids').val();
    var judul_file = $('#judulFile').val();

        $.ajaxFileUpload({
            url: 'controller/cont.upload_peg.php',
            secureuri: false,
            fileElementId: 'file_dokumen',
            dataType: 'json',
            data: {id_usulan: id_usulan, judul: judul_file},
            success: function(data, status)
            {
                if(data.msg){
                    $('#success3').html('File Berhasil Diupload').fadeIn(500).delay(2500).fadeOut(500);
                    get_dokumen(id_usulan);
                }else{
                    $('#error3').html(data.error).fadeIn(500).delay(2500).fadeOut(500);
                }
            }
        });
        return false;
}

function clear_upload(){
    document.getElementById('judul_file').value = "";
    document.getElementById('file_dokumen').value = "";
}

function uploadDokumen(id) {
    document.getElementById('ids').value = id;
    
    get_dokumen(id);
}


function get_dokumen(id_usulan){
    $.getJSON('controller/cont.dokumen.php',{aksi_list_dokumen_usulan_peg:true,id_usulan:id_usulan},function(data){
        $('#list_dokumen tbody').html('');
        var tab='';
        var i=1;
        if(data.msg){
            $.each(data.list_file,function(index,list_file){
                tab += "<tr>";
                tab += "<td>"+i+"</td>";
                tab += "<td>"+list_file.judul+"</td>";
                tab += "<td align=\"center\"><button type=\"button\"  class=\"btn btn-default btn-sm\" onclick=\"hapusFile("+list_file.id_dokumen+")\" ><span class=\"glyphicon glyphicon-remove-circle\"></span>&nbsp;Hapus</button></td>";
                tab += "<td align=\"center\"><a data-toggle=\"modal\" class=\"btn btn-default btn-sm\" onclick=\"download_file("+list_file.id_dokumen+")\"><span class=\"glyphicon glyphicon-saved\"></span>&nbsp;Lihat</a></td>";
                tab += "</tr>";
                i++;
            });
            $('#list_dokumen tbody').append(tab);
        }else{
            tab="<tr><td colspan=\"3\">Dokumen Kosong, Silahkan Upload File Anda</td></tr>";
            $('#list_dokumen tbody').append(tab);
        }
    });
}

function download_file(id_dokumen){
    $.getJSON('controller/cont.dokumen.php',{aksi_download:true,id_dokumen:id_dokumen},function(data){
        if(data.msg){  
            window.open(data.url,'Download');
        }else{
            $('#error3').html(data.error).fadeIn(500).delay(2500).fadeOut(500);
        }
    })  
}

function hapusFile(id_dokumen){
    $.getJSON('controller/cont.dokumen.php',{aksi_hapus_file:true,id_dokumen:id_dokumen},function(data){
        if(data.msg){
            $('#success3').html('Data Berhasil Dihapus').fadeIn(500).delay(2500).fadeOut(500);
            get_dokumen(data.id_usulan);
        }else{
            $('#error3').html(data.error).fadeIn(500).delay(2500).fadeOut(500);
        }
    });
}

function prosesUsulan(det){
    $.getJSON('controller/cont.usulan_kppn.php',{aksi_proses_usulan:true,ids:det},function(data){
        if(data.msg){
            get_daftar_pegawai();
            get_daftar_pegawai_sudah();
            getUsulan();
            $('#success').html('Usulan berhasil diproses,silahkan lihat di tab List Usulan').fadeIn(500).delay(2500).fadeOut(500);
        }else{
            $('#error').html("Gagal Proses Usulan").fadeIn(500).delay(2500).fadeOut(500);
        }
    });
}

function getUsulan(){
    $.getJSON('controller/cont.usulan_kppn.php', {aksi_list_usulan: true}, function(data) {
        $('#list_usulan tbody').html('');
        var tab = "";
        if (data.msg) {
            $.each(data.usulan, function(index, usulan) {
                tab = "<tr detailId=\""+ usulan.id +"\">";
                tab += "<td>" + usulan.tglProses + "</td>";
                tab += "<td>" + usulan.nomorSurat + "</td>";
                tab += "<td>" + usulan.tglSurat + "</td>";      
                tab += "<td align=\"center\"><a data-toggle=\"modal\" href=\"#myModal\" onclick=\"insert_pegawai(" + data.id_usulan + ")\" class=\"btn btn-default btn-sm\" ><span class=\"glyphicon glyphicon-saved\"></span>&nbsp;Daftar Pegawai</a></td>";
                tab += "<td align=\"center\"><a data-toggle=\"modal\" href=\"#myModalUpload\" onclick=\"uploadDokumen(" + data.id_usulan + ")\" class=\"btn btn-default btn-sm\" ><span class=\"glyphicon glyphicon-cloud-upload\"></span>&nbsp;Dokumen</a></td>";
                tab += "<td align=\"center\"><button onclick=\"cetak_usulan(" + data.id_usulan + ")\" class=\"btn btn-default btn-sm\" ><span class=\"glyphicon glyphicon-saved\"></span>&nbsp;Cetak</button></td>";
                tab += "<td align=\"center\"><button onclick=\"kirim_usulan(" + data.id_usulan + ")\" class=\"btn btn-default btn-sm\" ><span class=\"glyphicon glyphicon-saved\"></span>&nbsp;Kirim</button></td>";
                tab += "</tr>";
                //alert(tab);
                $('#list_usulan tbody').append(tab);

            });

            $('#list_usulan').dataTable({
                "sPaginationType": "bs_full",
                "bInfo": false,
                "bLengthChange": false,
                "iDisplayLength": 5,
              }); 
              $('#list_usulan').each(function(){
                var datatable = $(this);
                // SEARCH - Add the placeholder for Search and Turn this into in-line form control
                var search_input = datatable.closest('.dataTables_wrapper').find('div[id$=_filter] input');
                search_input.attr('placeholder', 'Search');
                search_input.addClass('form-control input-sm');
                // LENGTH - Inline-Form control
                var length_sel = datatable.closest('.dataTables_wrapper').find('div[id$=_length] select');
                length_sel.addClass('form-control input-sm');
              });
        } else {
            tab += "<tr>"
            tab += "<td colspan=\"9\">Data Tidak Ada</td>";
            tab += "</tr>";
            $('#list_usulan tbody').append(tab);
        }
    });
}

